package com.teacher.game.state;

import android.graphics.Color;
import android.view.MotionEvent;
import com.teacher.ellio.Assets;
import com.teacher.ellio.GameMainActivity;
import com.teacher.game.framework.util.Painter;
import com.teacher.game.framework.util.UIButton;
import com.teacher.game.framework.util.DifficultyPreferenceManager;

/**
 * 难度选择状态 - 让玩家选择游戏难度
 * 提供简单、一般、困难三种难度模式
 */
public class DifficultySelectState extends State {
    
    // 难度常量
    public static final int DIFFICULTY_EASY = 0;    // 简单
    public static final int DIFFICULTY_NORMAL = 1;  // 一般
    public static final int DIFFICULTY_HARD = 2;    // 困难
    
    // UI按钮
    private UIButton easyButton;
    private UIButton normalButton;
    private UIButton hardButton;
    private UIButton backButton;
    
    // 当前选中的难度
    private int selectedDifficulty = DIFFICULTY_NORMAL;
    
    // 难度偏好管理器
    private DifficultyPreferenceManager preferenceManager;
    
    // 动画效果
    private float titlePulse = 0;
    private float difficultyDescriptionAlpha = 1.0f;
    private boolean alphaIncreasing = false;
    
    @Override
    public void init() {
        // 初始化难度偏好管理器
        // 注意：在实际运行时需要传入正确的Context
        // preferenceManager = new DifficultyPreferenceManager(context);
        
        // 创建难度选择按钮 - 垂直排列，使用统一的按钮样式
        easyButton = new UIButton(300, 160, 500, 200, Assets.start, Assets.startDown);
        normalButton = new UIButton(300, 220, 500, 260, Assets.start, Assets.startDown);
        hardButton = new UIButton(300, 280, 500, 320, Assets.start, Assets.startDown);
        
        // 返回按钮 - 左下角
        backButton = new UIButton(50, 380, 150, 420, Assets.score, Assets.scoreDown);
        
        // 从持久化存储中读取上次选择的难度
        // 当有Context时，从偏好管理器加载
        // if (preferenceManager != null) {
        //     selectedDifficulty = preferenceManager.getSelectedDifficulty();
        // }
    }
    
    @Override
    public void update(float delta) {
        // 标题脉动动画
        titlePulse += delta * 2;
        
        // 难度描述闪烁动画
        if (alphaIncreasing) {
            difficultyDescriptionAlpha += delta * 2;
            if (difficultyDescriptionAlpha >= 1.0f) {
                difficultyDescriptionAlpha = 1.0f;
                alphaIncreasing = false;
            }
        } else {
            difficultyDescriptionAlpha -= delta * 2;
            if (difficultyDescriptionAlpha <= 0.3f) {
                difficultyDescriptionAlpha = 0.3f;
                alphaIncreasing = true;
            }
        }
        
        // 更新跑步动画
        Assets.runAnim.update(delta);
    }
    
    @Override
    public void render(Painter g) {
        // 渲染背景
        renderBackground(g);
        
        // 渲染标题
        renderTitle(g);
        
        // 渲染难度按钮和选项
        renderDifficultyButtons(g);
        
        // 渲染选中难度的描述
        renderSelectedDifficultyInfo(g);
        
        // 渲染返回按钮
        renderBackButton(g);
        
        // 渲染底部操作提示
        renderBottomInstructions(g);
    }
    
    /**
     * 渲染背景
     */
    private void renderBackground(Painter g) {
        // 使用欢迎背景
        g.drawImage(Assets.welcome, 0, 0);
        
        // 更深的半透明遮罩，提高文字可读性
        g.setColor(Color.argb(180, 0, 30, 60));
        g.fillRect(0, 0, GameMainActivity.GAME_WIDTH, GameMainActivity.GAME_HEIGHT);
        
        // 在中央区域添加更浅的背景框，突出难度选择区域
        g.setColor(Color.argb(80, 255, 255, 255));
        g.fillRect(250, 120, 300, 250);
    }
    
    /**
     * 渲染标题
     */
    private void renderTitle(Painter g) {
        // 标题阴影效果
        g.setColor(Color.argb(100, 0, 0, 0));
        g.drawString("选择游戏难度", 352, 62);
        
        // 标题正文 - 使用更醒目的颜色
        g.setColor(Color.rgb(255, 215, 0)); // 金色
        g.drawString("选择游戏难度", 350, 60);
        
        // 装饰性跑步小人 - 在标题上方，更对称的布局
        Assets.runAnim.render(g, 300, 25);
        Assets.runAnim.render(g, 450, 25);
    }
    
    /**
     * 渲染难度按钮和选项
     */
    private void renderDifficultyButtons(Painter g) {
        // 先渲染选中状态的背景高亮
        if (selectedDifficulty == DIFFICULTY_EASY) {
            g.setColor(Color.argb(120, 144, 238, 144));
            g.fillRect(295, 155, 210, 50);
        } else if (selectedDifficulty == DIFFICULTY_NORMAL) {
            g.setColor(Color.argb(120, 255, 255, 0));
            g.fillRect(295, 215, 210, 50);
        } else if (selectedDifficulty == DIFFICULTY_HARD) {
            g.setColor(Color.argb(120, 255, 99, 99));
            g.fillRect(295, 275, 210, 50);
        }
        
        // 渲染三个难度按钮
        easyButton.render(g);
        normalButton.render(g);
        hardButton.render(g);
        
        // 渲染按钮上的文字标签 - 使用阴影效果提高可读性
        // 简单难度
        g.setColor(Color.argb(100, 0, 0, 0)); // 阴影
        g.drawString("简单", 387, 187);
        g.setColor(selectedDifficulty == DIFFICULTY_EASY ? Color.rgb(255, 215, 0) : Color.WHITE);
        g.drawString("简单", 385, 185);
        
        // 一般难度
        g.setColor(Color.argb(100, 0, 0, 0)); // 阴影
        g.drawString("一般", 387, 247);
        g.setColor(selectedDifficulty == DIFFICULTY_NORMAL ? Color.rgb(255, 215, 0) : Color.WHITE);
        g.drawString("一般", 385, 245);
        
        // 困难难度
        g.setColor(Color.argb(100, 0, 0, 0)); // 阴影
        g.drawString("困难", 387, 307);
        g.setColor(selectedDifficulty == DIFFICULTY_HARD ? Color.rgb(255, 215, 0) : Color.WHITE);
        g.drawString("困难", 385, 305);
        
        // 在选中的按钮旁边绘制选中标识
        int selectedY = 0;
        switch (selectedDifficulty) {
            case DIFFICULTY_EASY: selectedY = 180; break;
            case DIFFICULTY_NORMAL: selectedY = 240; break;
            case DIFFICULTY_HARD: selectedY = 300; break;
        }
        
        g.setColor(Color.rgb(255, 215, 0));
        g.drawString("◄", 520, selectedY + 5);
        g.drawString("►", 270, selectedY + 5);
    }
    
    /**
     * 渲染选中难度的信息
     */
    private void renderSelectedDifficultyInfo(Painter g) {
        // 根据选中的难度显示简洁的描述
        String title = "";
        String description = "";
        int color = Color.WHITE;
        
        switch (selectedDifficulty) {
            case DIFFICULTY_EASY:
                title = "简单模式";
                description = "障碍物较慢，间距较大，适合新手";
                color = Color.rgb(144, 238, 144); // 浅绿色
                break;
                
            case DIFFICULTY_NORMAL:
                title = "一般模式";
                description = "标准游戏体验，平衡挑战";
                color = Color.YELLOW;
                break;
                
            case DIFFICULTY_HARD:
                title = "困难模式";
                description = "障碍物很快，间距较小，挑战极限";
                color = Color.rgb(255, 99, 99); // 浅红色
                break;
        }
        
        // 渲染难度标题 - 添加阴影效果
        g.setColor(Color.argb(100, 0, 0, 0));
        g.drawString(title, 352, 352);
        g.setColor(color);
        g.drawString(title, 350, 350);
        
        // 渲染描述 - 添加阴影效果
        g.setColor(Color.argb(100, 0, 0, 0));
        g.drawString(description, 282, 377);
        g.setColor(Color.WHITE);
        g.drawString(description, 280, 375);
    }
    
    /**
     * 渲染返回按钮
     */
    private void renderBackButton(Painter g) {
        backButton.render(g);
        // 返回按钮标签 - 添加阴影效果
        g.setColor(Color.argb(100, 0, 0, 0));
        g.drawString("返回", 87, 417);
        g.setColor(Color.WHITE);
        g.drawString("返回", 85, 415);
    }
    
    /**
     * 渲染底部操作提示
     */
    private void renderBottomInstructions(Painter g) {
        // 简化操作提示，避免文字重叠
        g.setColor(Color.argb(150, 192, 192, 192));
        g.drawString("点击难度按钮进行选择", 320, 410);
        
        // 更显眼的开始游戏提示
        g.setColor(Color.rgb(0, 255, 255)); // 青色
        g.drawString("选择完成后点击空白区域开始游戏", 290, 430);
    }
    
    @Override
    public boolean onTouch(MotionEvent e, int scaleX, int scaleY) {
        if (e.getAction() == MotionEvent.ACTION_DOWN) {
            // 处理按钮按下
            easyButton.onTouchDown(scaleX, scaleY);
            normalButton.onTouchDown(scaleX, scaleY);
            hardButton.onTouchDown(scaleX, scaleY);
            backButton.onTouchDown(scaleX, scaleY);
            
        } else if (e.getAction() == MotionEvent.ACTION_UP) {
            // 处理按钮释放
            if (easyButton.isPressed(scaleX, scaleY)) {
                easyButton.cancel();
                selectDifficulty(DIFFICULTY_EASY);
                return true;
                
            } else if (normalButton.isPressed(scaleX, scaleY)) {
                normalButton.cancel();
                selectDifficulty(DIFFICULTY_NORMAL);
                return true;
                
            } else if (hardButton.isPressed(scaleX, scaleY)) {
                hardButton.cancel();
                selectDifficulty(DIFFICULTY_HARD);
                return true;
                
            } else if (backButton.isPressed(scaleX, scaleY)) {
                backButton.cancel();
                onBackToMenu();
                return true;
                
            } else {
                // 点击其他区域开始游戏
                startGameWithSelectedDifficulty();
                
                // 取消所有按钮状态
                easyButton.cancel();
                normalButton.cancel();
                hardButton.cancel();
                backButton.cancel();
                return true;
            }
        }
        return true;
    }
    
    /**
     * 选择难度
     */
    private void selectDifficulty(int difficulty) {
        selectedDifficulty = difficulty;
        Assets.playSound(Assets.startId);
        
        // 保存到持久化存储
        if (preferenceManager != null) {
            preferenceManager.setSelectedDifficulty(difficulty);
        }
    }
    
    /**
     * 开始游戏（使用选中的难度）
     */
    private void startGameWithSelectedDifficulty() {
        Assets.playSound(Assets.startId);
        
        // 创建带难度参数的PlayState
        PlayState playState = new PlayState();
        playState.setDifficulty(selectedDifficulty);
        setCurrState(playState);
    }
    
    /**
     * 返回主菜单
     */
    private void onBackToMenu() {
        Assets.playSound(Assets.startId);
        setCurrState(new MenuState());
    }
    
    /**
     * 获取当前选中的难度
     */
    public int getSelectedDifficulty() {
        return selectedDifficulty;
    }
    
    /**
     * 设置难度
     */
    public void setSelectedDifficulty(int difficulty) {
        if (difficulty >= DIFFICULTY_EASY && difficulty <= DIFFICULTY_HARD) {
            this.selectedDifficulty = difficulty;
        }
    }
    
    /**
     * 获取难度名称
     */
    public static String getDifficultyName(int difficulty) {
        switch (difficulty) {
            case DIFFICULTY_EASY: return "简单";
            case DIFFICULTY_NORMAL: return "一般";
            case DIFFICULTY_HARD: return "困难";
            default: return "未知";
        }
    }
    
    /**
     * 获取难度描述
     */
    public static String getDifficultyDescription(int difficulty) {
        switch (difficulty) {
            case DIFFICULTY_EASY: return "慢速障碍物，大间距，高道具频率";
            case DIFFICULTY_NORMAL: return "标准速度，平衡体验";
            case DIFFICULTY_HARD: return "快速障碍物，小间距，低道具频率";
            default: return "标准难度";
        }
    }
}